26. Check for Understanding: For and While Loops
Check for Understanding
Time to pause and check your understanding about the important concepts of While Loops and For Loops.
SOLUTION:
- The condition for exiting the while loop should be included
- Check if the iteration condition is met
- Body of the loop should change the value of condition variables
Question: What type of loop should we use?
You need to write a loop that takes the numbers in a given list named num_list:
num_list = [422, 136, 524, 85, 96, 719, 85, 92, 10, 17, 312, 542, 87, 23, 86, 191, 116, 35, 173, 45, 149, 59, 84, 69, 113, 166]
Your code should add up the odd numbers in the list, but only up to the first 5 odd numbers together. If there are more than 5 odd numbers, you should stop at the fifth. If there are fewer than 5 odd numbers, add all of the odd numbers.
** Would you use a while
or a for
loop to write this code? **
We have provided our solution on the next page. Feel free to use the coding playground below to test your code.
Start Quiz:
## Please use this space to test and run your code